package ink.lovejinhu.leetcode;

/**
 * @author jinhu
 * created 2021-06-29 10:24
 */
public class Solution_0122 {
    /**
     * 买卖股票的最佳时机 II
     *
     * @param prices
     * @return
     */
    public int maxProfit(int[] prices) {
        int start = 0;  //买入时间
        int end = 0;   //卖出时间
        int profit = 0;
        int maxprofit = 0;
        for (;  end < prices.length; ) {
            if (prices[end] - prices[start] >= profit) {
                profit = prices[end] - prices[start];
                end++;
            } else {  //比之前小，证明开始下跌了
                maxprofit = profit + maxprofit;
                System.out.println("交易买入时间为:" + (start+1) + "------" + "交易卖出时间:" + ((end-1)+1));
                profit = 0;
                start = end;
                end++;
            }

        }

        return maxprofit+profit;
    }

    public static void main(String[] args) {
        Solution_0122 solution_0122 = new Solution_0122();
        int[] test = {7, 1, 5, 3, 6, 4};
//        System.out.println(solution_0122.maxProfit(test));
        int[] test1 = {1,2,3,4,5};
        System.out.println(solution_0122.maxProfit(test1));
    }
}
